package edu.whu.xterm.activity;

import edu.whu.xterm.R;
import edu.whu.xterm.http.RequestBuilder;
import greendroid.app.GDTabActivity;
import greendroid.widget.GActionBar.Type;
import greendroid.widget.GActionBarItem;
import greendroid.widget.LoaderActionBarItem;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Process;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainTabActivity extends GDTabActivity {

	public static final int ITEM_REFRESH = 1;

	private long firstClick;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getGActionBar().setType(Type.Empty);
		getGActionBar().addItem(greendroid.widget.GActionBarItem.Type.Refresh, ITEM_REFRESH);

		addTab(TabType.RECOMMEND.name(), R.string.tab_recommend, buildIntent(TabType.RECOMMEND));
		addTab(TabType.TOP10.name(), R.string.tab_top10, buildIntent(TabType.TOP10));
		addTab(TabType.FAVORITE.name(), R.string.tab_favorite, buildIntent(TabType.FAVORITE));
		addTab(TabType.BOARD.name(), R.string.tab_board, buildIntent(TabType.BOARD));
		addTab(TabType.MAIL.name(), R.string.tab_mail, buildIntent(TabType.MAIL));
		addTab(TabType.MORE.name(), R.string.tab_more, buildIntent(TabType.MORE));
		getTabHost().setCurrentTabByTag(TabType.TOP10.name());

	}

	@Override
	public boolean onHandleActionBarItemClick(GActionBarItem item, int position) {
		switch (item.getItemId()) {
		case ITEM_REFRESH: {
			Activity activity = getLocalActivityManager().getCurrentActivity();
			if (activity instanceof Top10WforumActivity) {
				((Top10WforumActivity) activity).onRefresh((LoaderActionBarItem) item);
				return true;
			}
			if (activity instanceof FavoriteActivity) {
				((FavoriteActivity) activity).onRefresh((LoaderActionBarItem) item);
			}
			break;
		}
		default:
			break;
		}
		return super.onHandleActionBarItemClick(item, position);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			long secondClick;
			if (firstClick == 0) {
				firstClick = System.currentTimeMillis();
			} else {
				secondClick = System.currentTimeMillis();
				long interval = secondClick - firstClick;
				if (interval < 2000) {
					firstClick = 0;
					RequestBuilder.getBuilder().logoutRequest();
					Process.killProcess(Process.myPid());
					return true;
				} else {
					firstClick = secondClick;
				}
			}
			Toast.makeText(getApplicationContext(), R.string.press_again_to_exit,
					Toast.LENGTH_SHORT).show();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected View createTabIndicator(String tag, CharSequence label, ViewGroup parent) {
		View view = getLayoutInflater().inflate(R.layout.main_tabwidget_layout, null);
		ImageView iv = (ImageView) view.findViewById(R.id.main_tabwidget_img);
		TextView tv = (TextView) view.findViewById(R.id.main_tabwidget_tx);
		TabType type = TabType.valueOf(tag);
		switch (type) {
		case RECOMMEND:
			iv.setImageResource(R.drawable.main_tab_recommend_selector);
			tv.setText(R.string.tab_recommend);
			break;
		case TOP10:
			iv.setImageResource(R.drawable.main_tab_top10_selector);
			tv.setText(R.string.tab_top10);
			break;
		case BOARD:
			iv.setImageResource(R.drawable.main_tab_board_selector);
			tv.setText(R.string.tab_board);
			break;
		case FAVORITE:
			iv.setImageResource(R.drawable.main_tab_favorite_selector);
			tv.setText(R.string.tab_favorite);
			break;
		case MAIL:
			iv.setImageResource(R.drawable.main_tab_message_selector);
			tv.setText(R.string.tab_mail);
			break;
		case MORE:
			break;
		default:
			break;
		}
		return view;
	}

	private Intent buildIntent(TabType type) {
		switch (type) {
		case RECOMMEND:
			return new Intent(getApplicationContext(), RecommendActivity.class);
		case TOP10:
			return new Intent(getApplicationContext(), Top10WforumActivity.class);
		case BOARD:
			return new Intent(getApplicationContext(), BoardsActivity.class);
		case FAVORITE:
			return new Intent(getApplicationContext(), FavoriteActivity.class);
		case MAIL:
			return new Intent(getApplicationContext(), MailActivity.class);
		case MORE:
			return new Intent(getApplicationContext(), MoreActivity.class);
		default:
			break;
		}
		return null;
	}

	private static enum TabType {
		RECOMMEND, //
		TOP10, //
		BOARD, //
		FAVORITE, //
		MAIL, //
		MORE, //
		;
	}

}
